package ilbunilcho.lol.parser;

import ilbunilcho.lol.exception.ChampionNotFoundException;
import ilbunilcho.lol.exception.ServerUnavailableException;
import ilbunilcho.lol.model.champion.Ability;
import ilbunilcho.lol.model.champion.Champion;
import ilbunilcho.lol.model.champion.RecommendedItems;
import ilbunilcho.lol.util.Util;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Iterator;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ChampionDetail {
	Logger L = Logger.getLogger(ChampionDetail.class);
	
	String baseUrl = null;
	String url = null;
	int champion = 0;
	HashMap<String, Object> ret = null;
	
	public ChampionDetail(String baseUrl, int champion) {
		this.baseUrl = baseUrl;
		this.champion = champion;
		this.url = baseUrl + "/champions/" + champion;
	}
	
	public void parse() throws ServerUnavailableException, ChampionNotFoundException {
		try {
			Document doc = Jsoup.connect(url).get();
			Element body = doc.body();
			
			Elements els = body.select("div.view_content_frame");

			if(els.isEmpty()) {
				throw new ChampionNotFoundException();
			}
			else {
				Element detail = els.get(0);
				
				Champion c = new Champion();
				
				ret = new HashMap<String, Object>();

				c.setName(detail.select("h2 > span.champion_name").text());
				c.setTitle(detail.select("h2 > span.champion_title").text());
				c.setPortrait(detail.select("td.champion_render > img").attr("src"));
				c.setDescription(detail.select("td.champion_description").text());
				
				els = detail.select("table.stats_table tr");
				
				String m;
				
				c.setDamage(Double.parseDouble(els.get(0).select("td.stats_value").text()));
				m = els.get(0).select("td.stats_modifier > span").text();
				m = (m == null || "".equals(m.trim())) ? "0" : m.replace(" / per level", "");
				c.setDamagePerLevel(Double.parseDouble(m));
				
				c.setHealth(Double.parseDouble(els.get(1).select("td.stats_value").text()));
				m = els.get(1).select("td.stats_modifier > span").text();
				m = (m == null || "".equals(m.trim())) ? "0" : m.replace(" / per level", "");
				c.setHealthPerLevel(Double.parseDouble(m));
				
				c.setMana(Double.parseDouble(els.get(2).select("td.stats_value").text()));
				m = els.get(2).select("td.stats_modifier > span").text();
				m = (m == null || "".equals(m.trim())) ? "0" : m.replace(" / per level", "");
				c.setManaPerLevel(Double.parseDouble(m));
				
				c.setMoveSpeed(Double.parseDouble(els.get(3).select("td.stats_value").text()));
				
				c.setArmor(Double.parseDouble(els.get(3).select("td.stats_value").text()));
				m = els.get(3).select("td.stats_modifier > span").text();
				m = (m == null || "".equals(m.trim())) ? "0" : m.replace(" / per level", "");
				c.setArmorPerLevel(Double.parseDouble(m));
				
				c.setSpellBlock(Double.parseDouble(els.get(4).select("td.stats_value").text()));
				m = els.get(4).select("td.stats_modifier > span").text();
				m = (m == null || "".equals(m.trim())) ? "0" : m.replace(" / per level", "");
				c.setSpellBlockPerLevel(Double.parseDouble(m));
				
				c.setHealthRegen(Double.parseDouble(els.get(5).select("td.stats_value").text()));
				m = els.get(5).select("td.stats_modifier > span").text();
				m = (m == null || "".equals(m.trim())) ? "0" : m.replace(" / per level", "");
				c.setHealthRegenPerLevel(Double.parseDouble(m));
				
				c.setManaRegen(Double.parseDouble(els.get(6).select("td.stats_value").text()));
				m = els.get(6).select("td.stats_modifier > span").text();
				m = (m == null || "".equals(m.trim())) ? "0" : m.replace(" / per level", "");
				c.setManaRegenPerLevel(Double.parseDouble(m));
				
				els = detail.select("table.items_table").first().select("td");//classic

				RecommendedItems r = new RecommendedItems();
				r.setOne(Integer.parseInt(els.get(0).select("a").attr("href").replace("/items#", "")));
				r.setTwo(Integer.parseInt(els.get(1).select("a").attr("href").replace("/items#", "")));
				r.setThree(Integer.parseInt(els.get(2).select("a").attr("href").replace("/items#", "")));
				r.setFour(Integer.parseInt(els.get(3).select("a").attr("href").replace("/items#", "")));
				r.setFive(Integer.parseInt(els.get(4).select("a").attr("href").replace("/items#", "")));
				r.setSix(Integer.parseInt(els.get(5).select("a").attr("href").replace("/items#", "")));
				
				c.setClassic(r);
				
				els = detail.select("table.items_table").last().select("td");//dominion
				r = new RecommendedItems();
				r.setOne(Integer.parseInt(els.get(0).select("a").attr("href").replace("/items#", "")));
				r.setTwo(Integer.parseInt(els.get(1).select("a").attr("href").replace("/items#", "")));
				r.setThree(Integer.parseInt(els.get(2).select("a").attr("href").replace("/items#", "")));
				r.setFour(Integer.parseInt(els.get(3).select("a").attr("href").replace("/items#", "")));
				r.setFive(Integer.parseInt(els.get(4).select("a").attr("href").replace("/items#", "")));
				r.setSix(Integer.parseInt(els.get(5).select("a").attr("href").replace("/items#", "")));
				
				c.setDominion(r);
				
				
//				for(int i=0; i<els.size(); i++) {
//					if(el.children().size() > 0) {
//						ret[cnt++] = el.select("a").attr("href") + "," + el.select("a").attr("href").replace("/items#", "") + "," + el.select("a > img").attr("src") + "," + el.select("a > img").attr("alt");
//					}
//					else {
//						continue;
//					}
//				}
//				els = detail.select("table.items_table").last().select("td");//dominion
//				for(Element el : els) {
//					if(el.children().size() > 0) {
//						ret[cnt++] = el.select("a").attr("href") + "," + el.select("a").attr("href").replace("/items#", "") + "," + el.select("a > img").attr("src") + "," + el.select("a > img").attr("alt");
//					}
//					else {
//						continue;
//					}
//				}
				els = detail.select("table.abilities_table tr");//ablities
				Ability a = new Ability();
				a.setName(els.get(0).select("td.ability_detail > span.ability_name").text());
				a.setDescription(els.get(0).select("td.ability_detail > span.ability_description").text());
				c.setAbilityBasic(a);
				
//				for(Element el : els) {
//					if(el.children().size() > 0) {
//						L.debug(">> " + el.select("img.ability_icon").attr("src"));
//						L.debug(">> " + el.select("td.ability_detail > span.ability_name").text());
//						L.debug(">> " + el.select("td.ability_detail > span.ability_description").text());
//						L.debug(">> " + Util.nvl(el.select("td.ability_detail > div.ability_effect").text()));
//						L.debug(">> " + Util.nvl(el.select("td.ability_detail > dl.ability_stats").get(0).child(0)));
//	//					L.debug(">> " + el.select("td.ability_detail dl.ability_stats").get(1).text());
//	//					L.debug(">> " + el.select("td.ability_detail dl.ability_stats").get(2).text());
//	//					L.debug(">> " + el.select("td.ability_detail dl.ability_stats").get(3).text());
//					}
//					else {
//						continue;
//					}
//				}
//				ret[cnt++] = detail.select("td.champion_description").text();
//				ret[cnt++] = detail.select("td.champion_description").text();
//				ret[cnt++] = detail.select("td.champion_description").text();
//				ret[cnt++] = detail.select("td.champion_description").text();
//				ret[cnt++] = detail.select("td.champion_description").text();
//				ret[cnt++] = detail.select("td.champion_description").text();
				
//				while(iter.hasNext()) {
//					Element el = iter.next();
//					
//					ret[cnt][0] = el.className().trim().replaceAll("filter_tag_", "").replaceAll(" ", ",");//tag
//					ret[cnt][1] = baseUrl + el.select("td.champion > a").attr("href");//link
//					ret[cnt][2] = el.select("td.champion > a > img").attr("src");//image
//					ret[cnt][3] = el.select("td.description > span > a").text();//name
//					ret[cnt][4] = el.select("td.statistics > table > tbody > tr").get(0).getElementsByAttributeValueMatching("class", ".+").size() + "";//attack
//					ret[cnt][5] = el.select("td.statistics > table > tbody > tr").get(1).getElementsByAttributeValueMatching("class", ".+").size() + "";//health
//					ret[cnt][6] = el.select("td.statistics > table > tbody > tr").get(2).getElementsByAttributeValueMatching("class", ".+").size() + "";//difficulty
//					ret[cnt][7] = el.select("td.statistics > table > tbody > tr").get(3).getElementsByAttributeValueMatching("class", ".+").size() + "";//spells
//
//					cnt++;
//				}

				try {
					JAXBContext context = JAXBContext.newInstance(Champion.class);
					Marshaller marshaller = context.createMarshaller();
					marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
					marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
					marshaller.marshal(c, System.out);
				} catch (JAXBException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}
		catch(IOException e) {
			throw new ServerUnavailableException();
		}
	}
	
	public HashMap getRotation() {
		return ret;
	}
	
	public static void main(String[] args) {
		PrintStream out = System.out;
		String baseUrl = "http://na.leagueoflegends.com/en";
		
		try {
			ChampionDetail cd = new ChampionDetail(baseUrl, 86);
			cd.parse();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
